<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Detection;
use App\Models\OrderGoodsModel;
use App\Models\sf_order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class DetectionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Detection(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('order_goods_sn');
            $grid->column('detection_img')->image();
            $grid->column('detection_status')->using(['1'=>'检测完毕','0'=>'正在检测']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Detection(), function (Show $show) {
            $show->field('id');
            $show->field('order_goods_sn');
            $show->field('detection_img');
            $show->field('detection_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Detection(), function (Form $form) {
            $form->display('id');
            $form->text('order_goods_sn')->value('');
            $form->image('detection_img')->saveFullUrl()->uniqueName()->required()->disk('qiniu');
            $form->hidden('detection_status')->value(1);
            $form->display('created_at');
            $form->display('updated_at');
            $form->submitted(function (Form $form) {
                if($form->detection_img){
                    $email='1764749557@qq.com';
                    Mail::raw($form->detection_img,function ($query)use($email){
                        $query->subject('体检报告')->to($email);
                    });
                }
            });
        });
    }
}
